/**
 * Created by vincent on 2017/9/14.
 */
moduleStudyMap.controller("AddStudyClassGroupController", function ($$CambioConfig, $scope, $log, $state, LoadingService, DialogService,
                                                                    UploadFileService, ConstantService, StudyClassGroupResource) {
    $scope.$on("ngDialog.opened", function () {
        dialogOnLoad();
    });

    $scope.newClassGroup = {
        isFree: false,
        active: true
    };

    /**
     * 上传图片
     * @param $files
     */
    $scope.uploadImage = function ($files) {
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope).then(function (o) {
                $scope.newClassGroup.picture = o.id;
            });
        }
    };

    $scope.cancel = function () {
        DialogService.close();
    };

    $scope.submit = function () {
        var currentStudyMapId = ConstantService.getItem("currentStudyMapId");
        if ($scope.addSCGForm.$valid && !StringUtils.isNlOrUndOrEmpty(currentStudyMapId)) {
            //LoadingService.show();
            $scope.newClassGroup.studyMap = {id: currentStudyMapId};
            StudyClassGroupResource.add($scope.newClassGroup, function (groupResult) {
                $log.debug("章节新增结果", groupResult);
                DialogService.close();
                $scope.groupList.push(groupResult);
                Alert.success("新增章节成功！");
                //LoadingService.hide();
            }, function () {
                //LoadingService.hide();
            });
        }
    };

});